package com.hhit.bigscreenbackmanage.service.impl.jsgl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.PageUtil;
import com.hhit.bigscreenbackmanage.dao.jsgl.TJsglSjzlwtMapper;
import com.hhit.bigscreenbackmanage.dao.jsgl.TJsglTysjBdzMapper;
import com.hhit.bigscreenbackmanage.entity.jsgl.*;
import com.hhit.bigscreenbackmanage.service.jsgl.TJsglSjzlwtService;
import com.hhit.bigscreenbackmanage.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TJsglSjzlwtServiceImpl implements TJsglSjzlwtService {

    private Logger log = LoggerFactory.getLogger(TJsglSjzlwtServiceImpl.class);

    @Autowired
    TJsglSjzlwtMapper tJsglSjzlwtMapper;

    @Override
    public Map<String,Object> insertTJsglSjzlwt(MultipartFile[] file, String dataTime, String propName) throws Exception {
        //删除同一时间数据
        JSONObject json = new JSONObject();
        json.put("dataTime",dataTime);
        tJsglSjzlwtMapper.deleteSjzlwtRecord(json);
        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TJsglSjzlwt> list = (List<TJsglSjzlwt>) Utils.mapToEntiry(result, TJsglSjzlwt.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TJsglSjzlwt tJsglSjzlwt : list) {
                        tJsglSjzlwt.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tJsglSjzlwt.setDataTime(dataTime);
                    }
                    tJsglSjzlwtMapper.insertTJsglSjzlwt(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String,Object> insertTJsglSjzlwtSjbg(MultipartFile[] file, String dataTime, String propName) throws Exception {
        //删除同一时间数据
        JSONObject json = new JSONObject();
        json.put("dataTime",dataTime);
        tJsglSjzlwtMapper.deleteSjzlwtSjbgRecord(json);
        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TJsglSjzlwtSjbg> list = (List<TJsglSjzlwtSjbg>) Utils.mapToEntiry(result, TJsglSjzlwtSjbg.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TJsglSjzlwtSjbg tJsglSjbg : list) {
                        tJsglSjbg.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tJsglSjbg.setDataTime(dataTime);
                    }
                    tJsglSjzlwtMapper.insertTJsglSjbg(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String,Object> insertTJsglSjzlwtYttb(MultipartFile[] file, String dataTime, String propName) throws Exception {
        //删除同一时间数据
        JSONObject json = new JSONObject();
        json.put("dataTime",dataTime);
        tJsglSjzlwtMapper.deleteSjzlwtYttbRecord(json);
        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TJsglSjzlwtYttb> list = (List<TJsglSjzlwtYttb>) Utils.mapToEntiry(result, TJsglSjzlwtYttb.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TJsglSjzlwtYttb tJsglSjzlwtYttb : list) {
                        tJsglSjzlwtYttb.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tJsglSjzlwtYttb.setDataTime(dataTime);
                    }
                    tJsglSjzlwtMapper.insertTJsglSjzlwtYttb(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String,Object> insertTJsglSjzlwtZlsj(MultipartFile[] file, String dataTime, String propName) throws Exception {
        //删除同一时间数据
        JSONObject json = new JSONObject();
        json.put("dataTime",dataTime);
        tJsglSjzlwtMapper.deleteSjzlwtZlsjRecord(json);
        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TJsglSjzlwtZlsj> list = (List<TJsglSjzlwtZlsj>) Utils.mapToEntiry(result, TJsglSjzlwtZlsj.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TJsglSjzlwtZlsj tJsglSjzlwtZlsj : list) {
                        tJsglSjzlwtZlsj.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tJsglSjzlwtZlsj.setDataTime(dataTime);
                    }
                    tJsglSjzlwtMapper.insertTJsglSjzlwtZlsj(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> querySjzlwtList(JSONObject jsonObject) throws Exception {

        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglSjzlwtMapper.querySjzlwtCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<TJsglSjzlwt> list = new ArrayList<>();
        try{
            list = tJsglSjzlwtMapper.querySjzlwtList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Object> queryCbsjCspswcList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglSjzlwtMapper.queryCbsjCspswcCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<TJsglSjzlwt> list = new ArrayList<>();
        try{
            list = tJsglSjzlwtMapper.queryCbsjCspswcList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Object> querySjzlwtSjbgList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglSjzlwtMapper.querySjzlwtSjbgCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<TJsglSjzlwtSjbg> list = new ArrayList<>();
        try{
            list = tJsglSjzlwtMapper.querySjzlwtSjbgList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Object> querySjzlwtYttbList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglSjzlwtMapper.querySjzlwtYttbCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<TJsglSjzlwtYttb> list = new ArrayList<>();
        try{
            list = tJsglSjzlwtMapper.querySjzlwtYttbList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Object> querySjzlwtZlsjList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglSjzlwtMapper.querySjzlwtZlsjCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<TJsglSjzlwtZlsj> list = new ArrayList<>();
        try{
            list = tJsglSjzlwtMapper.querySjzlwtZlsjList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Object> insertTJsglSjzlwtTj(MultipartFile[] file, String dataTime, String propName) throws Exception {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TJsglSjzlwtTj> list = (List<TJsglSjzlwtTj>) Utils.mapToEntiry(result, TJsglSjzlwtTj.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TJsglSjzlwtTj tJsglSjzlwtTj : list) {
                        tJsglSjzlwtTj.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tJsglSjzlwtTj.setDataTime(dataTime);
                    }
                    tJsglSjzlwtMapper.insertTJsglSjzlwtTj(list);
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> querySjzlwtTjList(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>();
        PageUtil page = new PageUtil();
        page.pageCount=Integer.parseInt(jsonObject.get("pageSize").toString());
        page.init(tJsglSjzlwtMapper.querySjzlwtTjCount(jsonObject),Integer.parseInt(jsonObject .get("pageNo").toString()));
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<TJsglSjzlwtTj> list = new ArrayList<>();
        try{
            list = tJsglSjzlwtMapper.querySjzlwtTjList(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        result.put("page",page);
        result.put("list",list);
        return result;
    }

    @Override
    public Map<String, Integer> querySjzlTjCount(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("dataTime") == null || "".equals(jsonObject.get("dataTime"))){
            throw new Exception("缺少必要参数: dataTime");
        }
        if("初步设计".equals(jsonObject.get("wtjd"))){
            jsonObject.put("sjly","省公司");
        }else {
            jsonObject.put("sjly","国网");
        }
        Map<String, Integer> result = new HashMap<>();
        try{
            result = tJsglSjzlwtMapper.querySjzlCount(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> querySjzlByParam(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("dataTime") == null || "".equals(jsonObject.get("dataTime"))){
            throw new Exception("缺少必要参数: dataTime");
        }
        if(StringUtils.isEmpty(jsonObject.get("type"))){
            throw new Exception("缺少必要参数: type");
        }
        List<Map<String, Object>> result = new ArrayList<>();
        try{
            result = tJsglSjzlwtMapper.querySjzlByParam(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        int x = 0;
        for (Map map:result) {
            if ("设计方案不当".equals(map.get("class"))){
                x = 1;
                break;
            }else {
                continue;
            }
        }
        if(x==0){
            Map map = new HashMap();
            map.put("class","设计方法不当");
            map.put("num",0);
            result.add(map);
        }
        if("wtcd".equals(jsonObject.get("type"))){
            Map<String,Object> map1 = new HashMap<>();
            map1.put("class","一般");
            map1.put("num",0);
            Map<String,Object> map2 = new HashMap<>();
            map2.put("class","重大");
            map2.put("num",0);
            Map<String,Object> map3 = new HashMap<>();
            map3.put("class","严重");
            map3.put("num",0);
            if(result.size() == 0){
                result.add(map1);
                result.add(map2);
                result.add(map3);
            }else {
                Map<String, List<Map<String, Object>>> listMap = result.stream().collect(Collectors.groupingBy(map -> map.get("class").toString()));
                List<Map<String, Object>> list = new ArrayList<>();
                if(listMap.get("一般") != null){
                    list.add(listMap.get("一般").get(0));
                }else{
                    list.add(map1);
                }
                if(listMap.get("重大") != null){
                    list.add(listMap.get("重大").get(0));
                }else{
                    list.add(map2);
                }
                if(listMap.get("严重") != null){
                    list.add(listMap.get("严重").get(0));
                }else{
                    list.add(map3);
                }
                result = list;
            }
        }
        return result;
    }

    @Override
    public Map<String, Object> getSjzlByParam_sjzy(JSONObject jsonObject) throws Exception {
        Map map = new HashMap();
        List list = new ArrayList();
        if(jsonObject.get("dataTime") == null || "".equals(jsonObject.get("dataTime"))){
            throw new Exception("缺少必要参数: dataTime");
        }
        List<Map<String, Object>> result = tJsglSjzlwtMapper.querySjzlByParam_sjzy(jsonObject);
        for (Map map1:result) {
            list.add(map1.get("num"));
        }
        map.put("ysjzlcbsj",list);
        return map;
    }

    @Override
    public Map<String, Integer> querySjbgCount(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("dataTime") == null || "".equals(jsonObject.get("dataTime"))){
            throw new Exception("缺少必要参数: dataTime");
        }
        Map<String, Integer> result = new HashMap<>();
        try{
            result = tJsglSjzlwtMapper.querySjbgCount(jsonObject);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new Exception("数据库内部错误");
        }
        return result;
    }

    @Override
    public void deleteSjzlwtRecord(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }
        try {
            tJsglSjzlwtMapper.deleteSjzlwtRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }

    @Override
    public void deleteSjzlwtSjbgRecord(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }
        try {
            tJsglSjzlwtMapper.deleteSjzlwtSjbgRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }

    @Override
    public void deleteSjzlwtYttbRecord(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }
        try {
            tJsglSjzlwtMapper.deleteSjzlwtYttbRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }

    @Override
    public void deleteSjzlwtZlsjRecord(JSONObject jsonObject) throws Exception {
        if(jsonObject.get("ids") == null || "".equals(jsonObject.get("ids"))){
            throw new Exception("缺少必要参数: ids");
        }
        try {
            tJsglSjzlwtMapper.deleteSjzlwtZlsjRecord(jsonObject);
        }catch (Exception e){
            throw new Exception("删除失败");
        }
    }
}
